// panel/index.js, this filename needs to match the one registered in package.json
const packageName = 'orcf_package';
const fs = require('fire-fs');
const path = require('fire-path');
var muban = require('./panel_muban');
Editor.Panel.extend({
    // css style for panel
    style: fs.readFileSync(Editor.url('packages://' + packageName + '/panel/index.css', 'utf8')) + "",

    // html template for panel
    template: fs.readFileSync(Editor.url('packages://' + packageName + '/panel/index.html', 'utf8')) + "",
  
    ready() {
        app = new window.Vue({
            el: this.shadowRoot,
            data: {
                layer_value:'',
                package_value:'',
                panel_value:'',
                persist_value:false
            },
            created(){
                this.layer_value = 'panelLayer';
                this.package_value = 'package_';
                this.panel_value = 'panel_';
                this.persist_value = false;                
                Editor.log('created');
            },
            methods:{
                btn_go(){                   
                    Editor.log('btn-go');                   
                    let url = Editor.Project.path + "/assets/package/" + this.package_value;
                    fs.readdir(url, (err1) =>{
                        if(!err1){
                            //存在                            
                            Editor.log('目录存在:',url);                            
                            let meta = Editor.assetdb.remote.loadMeta("db://assets/package/"+ this.package_value);    
                            if (!meta) {
                                Editor.log("meta文件不存在！");
                                return;
                            }   
                            if (!meta.isBundle) {
                                //将文件夹设置为AssetBundle
                                meta.isBundle = true;
                                meta.priority = 2;
                                Editor.log('设置bundle成功！',meta,meta.uuid);
                            }           
                            let url_panel = "db://assets/package/" + this.package_value+'/'+ this.panel_value+'.ts';;
                            Editor.log('生成ts中,请耐心等待下:',url_panel);    
                            
                            Editor.assetdb.create(url_panel, muban.muban_str(this.package_value,this.panel_value,this.layer_value)                         
                            ,(err)=>{
                                if (err) {
                                    console.log('write is error...',err);
                                }
                                else
                                {                                         
                                    Editor.log('生成ts成功');
                                }
                            })
                        }
                        else
                        {
                            Editor.log('目录不存在,请先用fairygui导入资源先');
                            /*
                            Editor.log('进行创建！',url);
                            fs.mkdir(url, { recursive: true }, (err2, results) => {
                                if (err2) Editor.log('创建目录出错:' + err2);
                                else {                                    
                                    //刷新编辑器
                                    Editor.assetdb.refresh('db://assets/package', (err, results)=> {                                        
                                        if(!err){
                                            Editor.log(url + '创建完毕！');                                             
                                            let meta = Editor.assetdb.remote.loadMeta("db://assets/package/"+ this.package_value);    
                                            if (!meta) {
                                                Editor.log("meta文件不存在！");
                                                return;
                                            } 
                                            if (!meta.isBundle) {
                                                //将文件夹设置为AssetBundle
                                                meta.isBundle = true;
                                                meta.priority = 2;
                                                Editor.log('设置bundle成功！',meta,meta.uuid);
                                            }     
                                            let url_panel = "db://assets/package/" + this.package_value+'/'+ this.panel_value+'.ts';;
                                            Editor.assetdb.create(url_panel,'const {ccclass, property} = cc._decorator;\n'+'@ccclass\n'+'export default class NewClass extends cc.Component {}'                            
                                            ,(err)=>{
                                                if (err) {
                                                    console.log('write is error...',err);
                                                }
                                                else
                                                {                                         
                                                    Editor.log('生成ts成功');
                                                }
                                            })
                                        }
                                    });
                                    
                                }
                            });
                            */
                        }
                    })
                }               
            },        
        })
    },
});